package com.freedom.monitor.myeye.storage.service;

import java.util.ArrayList;
import java.util.List;

import org.apache.thrift.TException;

import com.freedom.monitor.myeye.commmon.model.IOModel;
import com.freedom.monitor.myeye.commmon.model.KeyModel;
import com.freedom.monitor.myeye.commmon.utils.CompressUtils;
import com.freedom.rpc.thrift.common.utils.Logger;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;

//Phoenix    --->    http://www.cnblogs.com/smartloli/p/5047527.html
//AsyncHBase --->    
public class StorageServiceImpl implements StorageService.Iface {
	private static final Logger logger = Logger.getLogger(StorageServiceImpl.class);

	@SuppressWarnings("unchecked")
	@Override
	public boolean put(String data) throws TException {
		logger.debug("StorageServiceImpl.put ---> " + data);
		try {
			// 0收到了数据,校验
			if (null == data) {
				throw new Exception("data is null");
			}
			// 1)组织成IOModel对象
			Gson gson = new GsonBuilder().create();
			IOModel ioModel = gson.fromJson(data, IOModel.class);
			if (null == ioModel) {
				throw new Exception("ioModel --->  new Gson().fromJson(data, IOModel.class) fail ...");
			}
			// 2)取出内容
			String content = ioModel.getCt();
			if (null == content) {
				throw new Exception("content is null");
			}
			// 3)根据压缩算法修正出最终内容
			if (ioModel.isCp()) {
				if (CompressUtils.KAFKA_LZ4 != ioModel.getCa()) {
					throw new Exception("unknown compress algorithm--->" + ioModel.getCa());
				}
				content = CompressUtils.deCompress(content);
			}
			// 4)终于拿到了数据
			logger.debug(content);
			List<KeyModel> keyModels = gson.fromJson(content, ArrayList.class);
			logger.debug("keyModels--->" + keyModels);
			// 5)遍历
			String productName;
			String serviceName;
			String serverId;
			for (KeyModel keyModel : keyModels) {
				productName = keyModel.getPn();
				serviceName = keyModel.getSn();
				serverId = keyModel.getSid();
			}
		} catch (Exception e) {
			logger.error(e.toString());
		}

		return false;
	}

}
